package servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import Database.StaffDB;
import Model.Approval;
import Model.Committee;
import Model.CommitteeRole;

/**
 * Servlet implementation class CommitteeTable
 */
@WebServlet("/StaffappSearchResult")
public class StaffappSearchResult extends HttpServlet {
	private static final long serialVersionUID = 1L;
	JspWriter out;
	String id;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StaffappSearchResult() {
		super();
		// TODO Auto-generated constructor stub
	}

	public StaffappSearchResult(JspWriter outputStream, String id) {
		// TODO Auto-generated constructor stub
		out = outputStream;
		this.id = id;
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String[] arr = id.split(";");

		ArrayList<Approval>apptTable = new ArrayList<Approval>();
		for (int j = 0; j < arr.length; j++) {
			Approval appt = Database.StaffAppointmentDB
					.getAllApprovedAppointmentsByMD5(arr[j]);
			apptTable.add(appt);
		}

		for (int i = 0; i < apptTable.size(); i++) {
			String responsibility = "-";
			if (!apptTable.get(i).getDescription().equals(""))
				responsibility = apptTable.get(i).getDescription();

			out.print("<tr>");

			out.print("<td>");
			out.print(apptTable.get(i).getName());
			out.print("</td>");

			out.print("<td>");
			out.print(apptTable.get(i).getAppointment());
			out.print("</td>");

			out.print("<td>");
			String appointmentID = apptTable.get(i).getAppointmentID(); 
			String setJSFunction = " onClick=\"setPopup('" + appointmentID + "', event)\" ";
			out.print("<a href=\"#\" " + setJSFunction + ">");
			out.print("<img src=\" images/info_icon.png \" width=\"16px\" height=\"16px \">");
			out.print("<input id=\"getResponsiblity\" type = \"hidden\" value = \""+responsibility+"\">");
			out.print("</a>");			
			out.print("</td>");

			out.print("<td>");
			out.print(formatdate(apptTable.get(i).getDate()));
			out.print("</td>");

			out.print("<td>");
			out.print(apptTable.get(i).getApprovedBy());
			out.print("</td>");

			out.print("<td>");
			out.print("<a target=\"_blank\" href=\"ApptPrint?md5="
					+ apptTable.get(i).getMd5()
					+ "\">Print</a>");
			/*<a target=\"_blank\" href=\"ApprovedTablePrint?md5="
			+ apptTable.get(i).getMd5()
			+ "\">Print same appointment</a> */
			out.print("</td>");

			out.print("</tr>");

		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	private String formatdate(String date) {
		String result = date.split(" ")[0] + " " + date.split(" ")[1].charAt(0)
				+ date.split(" ")[1].substring(1).toLowerCase() + " "
				+ date.split(" ")[2];
		return result;
	}
}
